/*function getData(){
    var rows = [];
    for(var i=1; i<=800; i++){
        var amount = Math.floor(Math.random()*1000);
        var price = Math.floor(Math.random()*1000);
        rows.push({
            inv: 'Inv No '+i,
            date: $.fn.datebox.defaults.formatter(new Date()),
            name: 'Name '+i,
            amount: amount,
            price: price,
            cost: amount*price,
            note: 'Note '+i
        });
    }
    return rows;
}*/
$(function(){
    $('#defaultForm').validationEngine();
});

function reset(){
    $("#defaultForm")[0].reset();
    var zTree = $.fn.zTree.getZTreeObj("un_authority");
    zTree.checkAllNodes(false);
}

function close_tab(){
    var title="角色管理";
    var url="../../Admin/role/role_main";
    window.parent.removeTab(title,url);
}
function add_ajax_submit(){
    if(!$("form#defaultForm").validationEngine("validate")){
        return false;
    };

    var zTree = $.fn.zTree.getZTreeObj("un_authority");
    var zTreeChecked=zTree.getCheckedNodes(true);
    var length=zTreeChecked.length;
    if(length==0){
        alert("请选择权限");
        return;
    }
    var authority_id=new Array();
    for(var i=0;i<length;i++){
        authority_id[i]=zTreeChecked[i].id;
    }
    var role_name=$("#role_name").val();
    var role_code=$("#role_code").val();
    var data={"authority_id[]":authority_id,"role_name":role_name,"role_code":role_code};
    var url="../../Admin/Role/role_add_ajax";
    $.ajax({
        type: "post",
        async: true,
        traditional: true,
        data: data,
        url: url,
        success: function(d){
            var obj=$.parseJSON(d);
            if(obj.state==1){
                alert("新增角色成功！");
                window.parent.removeTab("角色管理","../../Admin/role/role_main");
            }else{
                alert("新增角色失败！");
                window.location.reload();
            }
        },
        error: function(e){
            alert("网络错误，请联系管理员");
            window.location.reload();
        }
    });
}

function edit_ajax_submit(){
    if(!$("form#defaultForm").validationEngine("validate")){
        return false;
    };

    var zTree = $.fn.zTree.getZTreeObj("un_authority");
    var zTreeChecked=zTree.getCheckedNodes(true);
    var length=zTreeChecked.length;
    if(length==0){
        alert("请选择权限");
        return;
    }
    var authority_id=new Array();
    for(var i=0;i<length;i++){
        authority_id[i]=zTreeChecked[i].id;
    }
    var role_name=$("#role_name").val();
    var role_code=$("#role_code").val();
    var role_id=$("#role_id").val();
    var data={"authority_id[]":authority_id,"role_name":role_name,"role_code":role_code,"role_id":role_id};
    var url="../../Admin/Role/role_edit_ajax";
    $.ajax({
        type: "post",
        async: true,
        traditional: true,
        data: data,
        url: url,
        success: function(d){
            console.log(d);
            var obj=$.parseJSON(d);
            if(obj.state==1){
                alert("编辑角色成功！");
                window.parent.removeTab("角色管理","../../Admin/role/role_main");
            }else{
                alert("编辑角色失败！");
                window.location.reload();
            }
        },
        error: function(e){
            alert("网络错误，请联系管理员");
            window.location.reload();
        }
    });
}